home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Draw Editor / Source / Palette.cpp < prev    next >
Encoding:
Text File  |  1995-12-11  |  24.0 KB  |  877 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Palette.cpp
  3.  
  4.     Contains:    Palette Classes Implementation
  5.  
  6.     Written by:    Dave Stafford
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. // -- Compiler/Preprocessor Switches --
  12.  
  13. #ifndef _COMPILERDEFS_
  14. #include "CompDefs.h"
  15. #endif
  16.  
  17. // -- DrawEditor Includes --
  18.  
  19. #ifndef _DRAWEDITORGLOBALS_
  20. #include "DrawEditorGlobals.h"
  21. #endif
  22.  
  23. #ifndef _DRAWEDITORCONSTANTS_
  24. #include "DrawEditorConstants.h"
  25. #endif
  26.  
  27. #ifndef _PALETTE_
  28. #include "Palette.h"
  29. #endif
  30.  
  31. #ifndef _DRAWEDITOR_
  32. #include "DrawEditor.h"
  33. #endif
  34.  
  35. #ifndef _DRAWEDITORUTILS_
  36. #include "DrawEditorUtils.h"
  37. #endif
  38.  
  39. // -- OpenDoc Includes --
  40.  
  41. #ifndef SOM_ODBinding_xh
  42. #include <ODBindng.xh>
  43. #endif
  44.  
  45. #ifndef SOM_ODTranslation_xh
  46. #include <Translt.xh>
  47. #endif
  48.  
  49. #ifndef SOM_ODShape_xh
  50. #include <Shape.xh>
  51. #endif
  52.  
  53. #ifndef SOM_ODFacet_xh
  54. #include <Facet.xh>
  55. #endif
  56.  
  57. #ifndef SOM_ODFrame_xh
  58. #include <Frame.xh>
  59. #endif
  60.  
  61. #ifndef SOM_ODWindow_xh
  62. #include <Window.xh>
  63. #endif
  64.  
  65. #ifndef SOM_ODWindowState_xh
  66. #include <WinStat.xh>
  67. #endif
  68.  
  69. #ifndef SOM_ODStorageUnit_xh
  70. #include <StorageU.xh>
  71. #endif
  72.  
  73. #ifndef SOM_ODSession_xh
  74. #include <ODSessn.xh>
  75. #endif
  76.  
  77. #ifndef SOM_ODInfo_xh
  78. #include <Info.xh>
  79. #endif
  80.  
  81. #ifndef SOM_ODPart_xh
  82. #include <Part.xh>
  83. #endif
  84.  
  85. // -- OpenDoc Utilities --
  86.  
  87. #ifndef _ODUTILS_
  88. #include "ODUtils.h"
  89. #endif
  90.  
  91. #ifndef _USERSRCM_
  92. #include "UseRsrcM.h"            // allow access to our library's resource fork
  93. #endif
  94.  
  95. #ifndef _ODMEMORY_
  96. #include "ODMemory.h"            // ODDisposePtr
  97. #endif
  98.  
  99. #ifndef _FOCUSLIB_
  100. #include <FocusLib.h>
  101. #endif
  102.  
  103. // -- Toolbox Includes --
  104.  
  105. #ifndef mathRoutinesIncludes
  106. #include <math routines.h>
  107. #endif
  108.  
  109. #ifndef __MEMORY__
  110. #include <memory.h>
  111. #endif
  112.  
  113.  
  114.  
  115. // **************************** Need to improve Handling of Failure **********************
  116.  
  117. //----------------------------------------------------------------------------------------
  118. //    FW_CColor::operator=
  119. //----------------------------------------------------------------------------------------
  120.  
  121. CRGBColor& CRGBColor::operator=(const RGBColor& rgb)
  122. {
  123.     red = rgb.red;
  124.     green = rgb.green;
  125.     blue = rgb.blue;
  126.     return (*this);
  127. }
  128.  
  129.  
  130. //----------------------------------------------------------------------------------------
  131. // CPalette::CPalette
  132. //----------------------------------------------------------------------------------------
  133.  
  134. CPalette::CPalette(DrawEditor* part)
  135. {
  136.     fSelectedPaletteItem = 0;
  137.     fDrawEditor = part;
  138.     fPresentation = kODNULL;
  139.     fODWindowID = kODNULLID;
  140. }
  141.  
  142.  
  143. //----------------------------------------------------------------------------------------
  144. // CPalette::~CPalette
  145. //----------------------------------------------------------------------------------------
  146.  
  147. CPalette::~CPalette()
  148. {
  149. }
  150.  
  151.  
  152. //----------------------------------------------------------------------------------------
  153. // CPalette::GetSelectedPaletteItem
  154. //----------------------------------------------------------------------------------------
  155.  
  156. ODSShort CPalette::GetSelectedPaletteItem() const
  157. {
  158.     return fSelectedPaletteItem;
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // CPalette::SetSelectedPaletteItem
  163. //----------------------------------------------------------------------------------------
  164.  
  165. void CPalette::SetSelectedPaletteItem(ODSShort paletteItem)
  166. {
  167.     fSelectedPaletteItem = paletteItem;
  168. }
  169.  
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // CPalette::GetPresentation
  173. //----------------------------------------------------------------------------------------
  174.  
  175. ODTypeToken CPalette::GetPresentation() const
  176. {
  177.     return fPresentation;
  178. }
  179.  
  180.  
  181. //----------------------------------------------------------------------------------------
  182. // CPalette::SetPresentation
  183. //----------------------------------------------------------------------------------------
  184.  
  185. void CPalette::SetPresentation(ODTypeToken presentation)
  186. {
  187.     fPresentation = presentation;
  188. }
  189.  
  190.  
  191. //----------------------------------------------------------------------------------------
  192. // CPalette::GetPaletteWindow
  193. //----------------------------------------------------------------------------------------
  194.  
  195. ODWindow* CPalette::GetPaletteWindow(Environment* ev)
  196. {
  197.     return fDrawEditor->GetSession(ev)->GetWindowState(ev)->AcquireWindow(ev, fODWindowID);
  198. }
  199.  
  200.  
  201. //----------------------------------------------------------------------------------------
  202. // CPalette::CreatePalette
  203. //----------------------------------------------------------------------------------------
  204.  
  205. void CPalette::CreatePalette(Environment* ev)
  206. {
  207.     
  208.     ODSession* session = fDrawEditor->GetSession(ev);
  209.     ODWindowState* windowState = session->GetWindowState(ev);
  210.     
  211.     // Setup Name
  212.     Str255 windowTitle = "\p";
  213.     
  214.     // Cache tokenized form of presentation
  215.     TokenizePalettePresentation(ev, session);
  216.     fPresentation = GetPresentation();
  217.     
  218.     Rect windowRect;
  219.     
  220.     GetPaletteRect(&windowRect);
  221.     
  222.     // Using the name and the calculated rectangle, create a new window.
  223.     // Note: that we are allocating the window record in temp mem using
  224.     // the OpenDoc memory mgr. This helps reduce app heap usage.
  225.     ODPlatformWindow platformWindow = kODNULL;
  226.     platformWindow = ::NewCWindow((Ptr)::ODNewPtr(sizeof(WindowRecord)), 
  227.                                 &windowRect, 
  228.                                 windowTitle, 
  229.                                 kODFalse, 
  230.                                 floatProc,
  231.                                 (WindowPtr)-1L, 
  232.                                 true, 
  233.                                 kODNULL);
  234.                                 
  235.     // If NewCWindow returned NULL it is possible that we are running on a pre 7.5
  236.     // system that does not support the new floatWindow window proc so,
  237.     // use our own..
  238.     if (platformWindow==kODNULL)
  239.     {
  240.         platformWindow = ::NewCWindow((Ptr)::ODNewPtr(sizeof(WindowRecord)), 
  241.                                     &windowRect, 
  242.                                     windowTitle, 
  243.                                     kODFalse, 
  244.                                     kFloatWindowDefID * 16,
  245.                                     (WindowPtr)-1L, 
  246.                                     true, 
  247.                                     kODNULL);
  248.         
  249.         // Release the window proc that was loaded for this window
  250.         // Then substitute our global one
  251.         Handle temp = ((WindowPeek)platformWindow)->windowDefProc;
  252.         ((WindowPeek)platformWindow)->windowDefProc = gGlobals->fFloatWindowProc;
  253.         ::ReleaseResource(temp);
  254.     }                                    
  255.     
  256.     
  257.     // ----- Create and return the OD window
  258.     ODWindow* window = windowState->RegisterWindow(ev,
  259.                                             platformWindow, 
  260.                                             kODNonPersistentFrameObject,
  261.                                             kODFalse,                // root window
  262.                                             kODFalse,                // On the Mac we handle the resizing ourself
  263.                                             kODTrue,                // Is a floating window
  264.                                             false,                    // shouldSave 
  265.                                             kODFalse,                // Allow OpenDoc to dispose this window?
  266.                                             fDrawEditor->GetODPart(),
  267.                                               session->Tokenize(ev, kODViewAsFrame),
  268.                                             fPresentation,
  269.                                             kODNULL);
  270.     THROW_IF_NULL(window);
  271.     
  272.     fODWindowID = window->GetID(ev);
  273.     ODReleaseObject(ev, window);
  274.     
  275. }
  276.  
  277. //----------------------------------------------------------------------------------------
  278. // CPalette::ClosePalette
  279. //----------------------------------------------------------------------------------------
  280. void CPalette::ClosePalette(Environment* ev)
  281. {
  282.     // Get the window reference for the palette, if there is one
  283.     ODWindow* window = this->GetPaletteWindow(ev);
  284.         
  285.     if (window)
  286.     {
  287.         // Close and Release the window
  288.         ODReleaseObject(ev, window);
  289.         window->CloseAndRemove(ev);
  290.         fODWindowID = kODNULLID;
  291.     }
  292. }
  293.  
  294. //----------------------------------------------------------------------------------------
  295. // CPalette::IsShown
  296. //----------------------------------------------------------------------------------------
  297. ODBoolean CPalette::IsShown(Environment* ev)
  298. {
  299.     ODBoolean shown = kODFalse;
  300.     
  301.     // Get the window reference for the palette, if there is one
  302.     ODWindow* window = this->GetPaletteWindow(ev);
  303.     
  304.     if (window==kODNULL)
  305.     {
  306.         return kODFalse;
  307.     }
  308.         
  309.     shown = window->IsShown(ev);
  310.     
  311.     // Release the acquired window
  312.     ODReleaseObject(ev, window);
  313.     
  314.     return shown;
  315. }
  316.  
  317. //----------------------------------------------------------------------------------------
  318. // CPalette::ShowPalette
  319. //----------------------------------------------------------------------------------------
  320.  
  321. void CPalette::ShowPalette(Environment* ev, ODBoolean show)
  322. {
  323.     // Get the window reference for the palette, if there is one
  324.     ODWindow* window = this->GetPaletteWindow(ev);
  325.     
  326.     if (show&&!this->IsShown(ev))
  327.     {
  328.         if (window==kODNULL)
  329.         {
  330.             CreatePalette(ev);
  331.             
  332.             window = this->GetPaletteWindow(ev);
  333.                         
  334.             // If we don't have a window at this point then something is wrong
  335.             THROW_IF_NULL(window);
  336.             window->Open(ev);
  337.         }
  338.         
  339.         window->Show(ev);
  340.     }
  341.     else
  342.     if (!show&&this->IsShown(ev))
  343.     {
  344.         if (window!=kODNULL)
  345.             window->Hide(ev);
  346.     }
  347.     
  348.     // Release the acquired window
  349.     ODReleaseObject(ev, window);
  350. }
  351.  
  352. //==============================================================================
  353. // CColorPalette
  354. //==============================================================================
  355.  
  356. const short CColorPalette::kColorPaletteHeight = 170;
  357. const short CColorPalette::kColorPaletteWidth = 170;
  358. const short CColorPalette::kColorSwatchAreaOffset = 4;
  359. const short CColorPalette::kColorSwatchSize = 11;
  360.  
  361. //----------------------------------------------------------------------------------------
  362. // CColorPalette::CColorPalette
  363. //----------------------------------------------------------------------------------------
  364.  
  365. CColorPalette::CColorPalette(DrawEditor* part) : CPalette(part),
  366.     fColorTable(NULL)
  367. {
  368.     fColorTable = ::GetCTable(72);
  369.     fNumbersOfColors = 256;
  370.     fSavedInvalidRect.top        = ff(kColorSwatchAreaOffset);
  371.     fSavedInvalidRect.left        = ff(kColorSwatchAreaOffset);
  372.     fSavedInvalidRect.bottom    = ff(kColorSwatchAreaOffset + kColorSwatchSize);
  373.     fSavedInvalidRect.right        = ff(kColorSwatchAreaOffset + kColorSwatchSize);
  374. }
  375.  
  376.  
  377. //----------------------------------------------------------------------------------------
  378. // CColorPalette::~CColorPalette
  379. //----------------------------------------------------------------------------------------
  380.  
  381. CColorPalette::~CColorPalette()
  382. {
  383.     if (fColorTable)
  384.         ::DisposeCTable(fColorTable);
  385. }
  386.  
  387. //----------------------------------------------------------------------------------------
  388. // CColorPalette::GetColor
  389. //----------------------------------------------------------------------------------------
  390.  
  391. void CColorPalette::GetColor(short colorIndex, CRGBColor* color) const
  392. {
  393.     *color = ((*fColorTable)->ctTable[colorIndex].rgb);
  394. }
  395.  
  396. //----------------------------------------------------------------------------------------
  397. // CColorPalette::NumberOfColors
  398. //----------------------------------------------------------------------------------------
  399.  
  400. ODSShort CColorPalette::NumberOfColors() const
  401. {
  402.     return fNumbersOfColors;
  403. }
  404.  
  405. //----------------------------------------------------------------------------------------
  406. // CColorPalette::GetPaletteRect
  407. //----------------------------------------------------------------------------------------
  408.  
  409. void CColorPalette::GetPaletteRect(Rect* r)
  410. {
  411.     SetRect(r, 80, 80, 80 + kColorPaletteWidth, 80 + kColorPaletteHeight);
  412. }
  413.  
  414. //----------------------------------------------------------------------------------------
  415. // CColorPalette::TokenizePalettePresentation
  416. //----------------------------------------------------------------------------------------
  417.  
  418. void CColorPalette::TokenizePalettePresentation(Environment* ev, ODSession* session)
  419. {
  420.     SetPresentation(session->Tokenize(ev, kColorPalettePresentation));
  421. }
  422.  
  423. //----------------------------------------------------------------------------------------
  424. // CColorPalette::Draw
  425. //----------------------------------------------------------------------------------------
  426.  
  427. void CColorPalette::Draw(Environment* ev, ODFacet* facet, ODShape* invalidShape)
  428. {
  429.     ODSShort    x;
  430.     Rect        box, colorbox;
  431.     CRGBColor    saveColor, swatchColor;
  432.  
  433.     const short kPenWidth = 1;
  434.     
  435.     CFocus using(ev, facet);
  436.     
  437.     // Get frame bounds for drawing.
  438.     ODShape* frameShape = facet->GetFrame(ev)->AcquireFrameShape(ev, kODNULL);
  439.     RgnHandle frameRgn = (frameShape->GetQDRegion(ev));
  440.     Rect frameRect = (**frameRgn).rgnBBox;
  441.     ODReleaseObject(ev, frameShape);
  442.     
  443.     SetRect(&box, kColorSwatchAreaOffset, kColorSwatchAreaOffset, kColorSwatchSize + kColorSwatchAreaOffset, kColorSwatchSize +  kColorSwatchAreaOffset);
  444.     
  445.     for ( x = 0; x < fNumbersOfColors; x++ )
  446.     {
  447.         Point boxCorner = {box.bottom,box.right};
  448.         if (!PtInRgn(boxCorner, frameRgn))
  449.         {
  450.             OffsetRect(&box, 0, kColorSwatchSize - kPenWidth );
  451.             box.left = kColorSwatchAreaOffset;
  452.             box.right = kColorSwatchSize + kColorSwatchAreaOffset;
  453.         }
  454.         
  455.         FrameRect(&box);
  456.         
  457.         colorbox = box;
  458.         
  459.         GetForeColor((RGBColor*)&saveColor);
  460.         GetColor(x, &swatchColor);
  461.         RGBForeColor((RGBColor*)&swatchColor);
  462.         
  463.         InsetRect(&colorbox, kPenWidth, kPenWidth);
  464.         PaintRect(&colorbox);
  465.         
  466.         // Hilight the selected item
  467.         if (x == GetSelectedPaletteItem())
  468.         {
  469.             CRGBColor black(0,0,0);
  470.             RGBForeColor((RGBColor*)&black);
  471.             
  472.             PenState savePen;
  473.             GetPenState(&savePen);
  474.             PenSize(2, 2);
  475.             FrameRect(&box);
  476.             SetPenState(&savePen);
  477.         }
  478.  
  479.         RGBForeColor((RGBColor*)&saveColor);
  480.     
  481.         OffsetRect(&box, kColorSwatchSize - kPenWidth, 0);
  482.     }
  483.     
  484. }
  485.  
  486. //----------------------------------------------------------------------------------------
  487. // CColorPalette::DoMouseDownInPalette
  488. //----------------------------------------------------------------------------------------
  489.  
  490. ODBoolean CColorPalette::DoMouseDownInPalette(Environment* ev, 
  491.                                                 ODFrame* frame, 
  492.                                                 Point where, 
  493.                                                 ODSShort* chosenItem,
  494.                                                 ODBoolean changePalette)
  495. {
  496.     ODSShort savedPaletteItem = GetSelectedPaletteItem();
  497.     ODSShort newPaletteItem;
  498.     Rect mouseDownRect;
  499.     ODSShort rowSize = ( kColorPaletteWidth - kColorSwatchAreaOffset ) / kColorSwatchSize +1;
  500.     
  501.     // Eliminate the border area
  502.     ODSShort horz = where.h - kColorSwatchAreaOffset;
  503.     ODSShort vert = where.v - kColorSwatchAreaOffset;
  504.  
  505.     // Initialize chosenItem return parameter
  506.     if (chosenItem)
  507.         *chosenItem = 0;
  508.     
  509.     // Do nothing if a grid line was clicked in
  510.     if ( (!horz % kColorSwatchSize)||(!horz % kColorSwatchSize))
  511.         return kODFalse;
  512.     
  513.     // eliminate the pen lines
  514.     horz = horz - horz / kColorSwatchSize;
  515.     vert = vert - vert / kColorSwatchSize;
  516.     
  517.     // Find the row / column
  518.     ODSShort  column = horz / ( kColorSwatchSize - 2 );
  519.     ODSShort row =  vert / ( kColorSwatchSize - 2 );
  520.     
  521.     // New selected palette item
  522.     newPaletteItem = row * rowSize + column;
  523.         
  524.     // Define the clicked rectangle
  525.     SetRect( &mouseDownRect,    column * kColorSwatchSize,
  526.                                 row * kColorSwatchSize,
  527.                                 (column + 1) * kColorSwatchSize,
  528.                                 (row + 1) * kColorSwatchSize ); 
  529.                                 
  530.     OffsetRect( &mouseDownRect, kColorSwatchAreaOffset - column, kColorSwatchAreaOffset - row );
  531.     
  532.     // Set the chosen color parameter
  533.     if (chosenItem)
  534.         *chosenItem = newPaletteItem;
  535.     
  536.     if (newPaletteItem==savedPaletteItem) 
  537.         return kODFalse;
  538.         
  539.     ODRect    invalidRect;
  540.     
  541.     // Only make changes to the palette if changePalette parameter is true
  542.     if (changePalette == kODTrue)
  543.     {
  544.         // Invalidate the old color swatch to force redrawing of the
  545.         // tool highlighting
  546.             
  547.         ODShape* invalidShape = frame->CreateShape(ev);
  548.         invalidShape->SetRectangle(ev, &fSavedInvalidRect);
  549.         
  550.         frame->Invalidate(ev, invalidShape, kODNULL); // Pass NULL for the bias Canvas
  551.             
  552.         // Release the ref counted object
  553.         ODReleaseObject(ev, invalidShape);
  554.         invalidShape = kODNULL;
  555.         
  556.         // Invalidate the new color swatch to force redrawing of the
  557.         // tool highlighting
  558.         invalidRect.top        = ff(mouseDownRect.top);
  559.         invalidRect.left    = ff(mouseDownRect.left);
  560.         invalidRect.bottom    = ff(mouseDownRect.bottom);
  561.         invalidRect.right    = ff(mouseDownRect.right);
  562.         
  563.         invalidShape = frame->CreateShape(ev);
  564.         invalidShape->SetRectangle(ev, &invalidRect);
  565.         
  566.         frame->Invalidate(ev, invalidShape, kODNULL); // Pass NULL for the bias Canvas
  567.             
  568.         // Release the ref counted object
  569.         ODReleaseObject(ev, invalidShape);
  570.         invalidShape = kODNULL;
  571.         
  572.         // Set current item
  573.         SetSelectedPaletteItem(newPaletteItem);
  574.         
  575.         // Save the new rect to be invalidated for the next change
  576.         fSavedInvalidRect = invalidRect;
  577.     }
  578.     
  579.     return kODTrue;
  580. }
  581.  
  582. //==============================================================================
  583. // CToolPalette
  584. //==============================================================================
  585.  
  586. const short CToolPalette::kToolPaletteHeight = 80;
  587. const short CToolPalette::kToolPaletteWidth = 54;
  588. const short CToolPalette::kPaletteBasePictureID = kBaseResourceID;
  589. const short CToolPalette::kToolCount = 7;
  590. const short CToolPalette::kToolSize = 26;
  591. const short CToolPalette::kToolOffset = 2;
  592.  
  593. //----------------------------------------------------------------------------------------
  594. // CToolPalette::CToolPalette
  595. //----------------------------------------------------------------------------------------
  596.  
  597. CToolPalette::CToolPalette(DrawEditor* part) : CPalette(part)
  598. {
  599.     fToolPicts = kODNULL;
  600.     fTextToolActive = kODFalse;
  601.     
  602.     // Default to selection tool
  603.     SetSelectedPaletteItem(kSelectionTool);
  604. }
  605.  
  606.  
  607. //----------------------------------------------------------------------------------------
  608. // CToolPalette::~CToolPalette
  609. //----------------------------------------------------------------------------------------
  610.  
  611. CToolPalette::~CToolPalette()
  612. {
  613.     if (fToolPicts)    
  614.     {
  615.         for ( ODSShort index = 0; index < ( kToolCount + 1 ); index ++)
  616.         {
  617.             PicHandle pict = fToolPicts[index];
  618.             if (pict)
  619.                 ::ODDeletePicture(pict);
  620.         }
  621.         ::ODDisposePtr(fToolPicts);
  622.         fToolPicts = kODNULL;
  623.     }
  624. }
  625.  
  626.  
  627.  
  628. //----------------------------------------------------------------------------------------
  629. // CToolPalette::AdjustTextTool
  630. //----------------------------------------------------------------------------------------
  631.  
  632. void CToolPalette::AdjustTextTool(Environment* ev)
  633. {
  634.     //  Check to see if there is actually a text editor available
  635.     ODSession* session = fDrawEditor->GetSession(ev);
  636.     ODBoolean editorIsAvailable;
  637.  
  638.     TRY
  639.         ODEditor editorID = session->GetBinding(ev)->
  640.                     ChooseEditorForPart(ev, kODNULL, gGlobals->fCurrentTextPartKind);
  641.         
  642.         editorIsAvailable = strcmp(editorID, kODBlackBoxHandlerOfLastResort);
  643.     CATCH_ALL
  644.         editorIsAvailable = kODFalse;
  645.     ENDTRY
  646.     
  647.     // If we concur with reality, then return
  648.     if (editorIsAvailable == fTextToolActive)
  649.         return;
  650.     
  651.     // Adjust our flag
  652.     fTextToolActive = editorIsAvailable;
  653.  
  654.     // Invalidate tool
  655.     this->ActivateTool(ev, kTextTool);    
  656.     
  657. }
  658. //----------------------------------------------------------------------------------------
  659. // CToolPalette::CreatePalette
  660. //----------------------------------------------------------------------------------------
  661.  
  662. void CToolPalette::CreatePalette(Environment* ev)
  663. {
  664.     ODSLong        savedRefNum;
  665.  
  666.     
  667.     // Get and Detach the resource before we
  668.     // release the resource file
  669.     
  670.     savedRefNum = BeginUsingLibraryResources();
  671.     
  672.     // Load the main palette picture and the six tool picts 
  673.     fToolPicts = (PicHandle*) ::ODNewPtrClear(sizeof(PicHandle) * (kToolCount + 1));
  674.     THROW_IF_NULL(fToolPicts);
  675.     
  676.     for ( ODSShort index = 0; index < kToolCount + 1; index ++)
  677.     {
  678.         PicHandle pict = ::ODLoadPicture(kPaletteBasePictureID+index);
  679.         
  680.         if (pict==kODNULL)
  681.         {
  682.             // There is a bug in ResError, when resources are not found,
  683.             // which may cause noErr to be returned. If that is the case,
  684.             // we throw resNotFound.
  685.             THROW_IF_ERROR((ODError)ResError());
  686.             THROW(resNotFound);
  687.         }
  688.         fToolPicts[index] = pict;
  689.     }
  690.         
  691.     EndUsingLibraryResources(savedRefNum);    
  692.     
  693.     // Set the text tool flag
  694.     this->AdjustTextTool(ev);
  695.     
  696.     SetSelectedPaletteItem(1);
  697.             
  698.     CPalette::CreatePalette(ev);
  699. }
  700.  
  701.  
  702. //----------------------------------------------------------------------------------------
  703. // CToolPalette::GetPaletteRect
  704. //----------------------------------------------------------------------------------------
  705.  
  706. void CToolPalette::GetPaletteRect(Rect* r)
  707. {
  708.     SetRect(r, 460, 80, 460 + kToolPaletteWidth, 80 + kToolPaletteHeight);
  709. }
  710.  
  711. //----------------------------------------------------------------------------------------
  712. // CToolPalette::GetToolRect
  713. //----------------------------------------------------------------------------------------
  714.  
  715. void CToolPalette::GetToolRect(short tool, Rect* rect)
  716. {
  717.     // Find the column & row
  718.     ODSShort col = !(tool % 2);
  719.     ODSShort row = tool / 2 - ( tool + 1) % 2;
  720.     
  721.     SetRect( rect, col * kToolSize + kToolOffset - col,
  722.                     row * kToolSize + kToolOffset - row,
  723.                      col * kToolSize + kToolSize + kToolOffset - col,
  724.                       row * kToolSize + kToolSize + kToolOffset - row );
  725. }
  726.  
  727. //----------------------------------------------------------------------------------------
  728. // CToolPalette::TokenizePalettePresentation
  729. //----------------------------------------------------------------------------------------
  730.  
  731. void CToolPalette::TokenizePalettePresentation(Environment* ev, ODSession* session)
  732. {
  733.     SetPresentation(session->Tokenize(ev, kToolPalettePresentation));
  734. }
  735.  
  736. //----------------------------------------------------------------------------------------
  737. // CToolPalette::Draw
  738. //----------------------------------------------------------------------------------------
  739.  
  740. void CToolPalette::Draw(Environment* ev, ODFacet* facet, ODShape* invalidShape)
  741. {
  742.     CFocus using(ev, facet);
  743.  
  744.     // Draw the main tool picture
  745.     Rect bounds = (**(PicHandle) fToolPicts[0]).picFrame;
  746.     OffsetRect(&bounds, -bounds.left, -bounds.top);
  747.     DrawPicture((PicHandle) fToolPicts[0], &bounds);
  748.     
  749.     // Highlight the active tool
  750.     ODSShort selectedTool = GetSelectedPaletteItem();
  751.     GetToolRect( selectedTool, &bounds );
  752.     
  753.     // Draw the inverted tool picture for the selected tool
  754.     DrawPicture((PicHandle) fToolPicts[selectedTool], &bounds);
  755.     
  756.     // If the text part is not available then display the 
  757.     // inactive text tool button
  758.     if (!fTextToolActive)
  759.     {
  760.         // Draw the disabled text tool pict over the active one.
  761.         GetToolRect( kTextTool, &bounds );
  762.         DrawPicture((PicHandle) fToolPicts[kInactiveTextTool], &bounds);
  763.     }
  764.  
  765. }
  766.  
  767.         
  768. //----------------------------------------------------------------------------------------
  769. // CToolPalette::InvalidateTool
  770. //----------------------------------------------------------------------------------------
  771.  
  772. void CToolPalette::InvalidateTool(Environment* ev, ODSShort whichTool)
  773. {
  774.     Rect whichToolRect;
  775.     
  776.     // Window & Frame
  777.     ODWindow*    tWindow = this->GetPaletteWindow(ev);
  778.     THROW_IF_NULL(tWindow);
  779.     
  780.     ODFrame*    paletteFrame = tWindow->GetRootFrame(ev);
  781.     
  782.     // Get the picts bounds
  783.     GetToolRect( whichTool, &whichToolRect );
  784.     
  785.     // Convert to an ODRect and call frame::invalidate
  786.     ODRect invalidRect(whichToolRect);
  787.     ODShape* invalidShape = paletteFrame->CreateShape(ev);
  788.     invalidShape->SetRectangle(ev, &invalidRect);
  789.     paletteFrame->Invalidate(ev, invalidShape, kODNULL);
  790.  
  791.     // Release acquired geometry
  792.     ODReleaseObject(ev, invalidShape);
  793.     
  794.     // Release acquired window
  795.     ODReleaseObject(ev, tWindow);
  796. }
  797.  
  798. //----------------------------------------------------------------------------------------
  799. // CToolPalette::ActivateTool
  800. //----------------------------------------------------------------------------------------
  801.  
  802. void CToolPalette::ActivateTool(Environment* ev, ODSShort whichTool)
  803. {
  804.     // If the palette isn't shown then we can't operate on the window
  805.     // because it hasn't yet been loaded.
  806.     if (!this->IsShown(ev))
  807.         return;
  808.     
  809.     // If they want to activate the text tool and it isn't available then bail.
  810.     if (!fTextToolActive && whichTool == kTextTool)
  811.         return;
  812.     
  813.     // Invalidate the old tool
  814.     this->InvalidateTool(ev, this->GetSelectedPaletteItem());
  815.     
  816.     // Invalidate the new tool
  817.     this->InvalidateTool(ev, whichTool);
  818.  
  819.     // Change the current tool
  820.     SetSelectedPaletteItem(whichTool);
  821. }
  822.  
  823.  
  824. //----------------------------------------------------------------------------------------
  825. // CToolPalette::DoMouseDownInPalette
  826. //----------------------------------------------------------------------------------------
  827.  
  828. ODBoolean CToolPalette::DoMouseDownInPalette(Environment* ev, 
  829.                                                 ODFrame* frame, 
  830.                                                 Point where, 
  831.                                                 ODSShort* chosenItem,
  832.                                                 ODBoolean changePalette)
  833. {
  834.     ODSShort savedPaletteItem = GetSelectedPaletteItem();
  835.     
  836.     for (ODSShort i = 1; i <= kToolCount; i++)
  837.     {
  838.         
  839.         Rect checkBounds;
  840.         ODSShort column = i % 2;
  841.         ODSShort row = i / 2 - i % 2;
  842.         
  843.         GetToolRect( i, &checkBounds );
  844.         
  845.         // Set the chosenItem return parameter
  846.         if (chosenItem)
  847.             *chosenItem = i;
  848.         
  849.         if (changePalette == kODTrue)
  850.         {
  851.             if ( PtInRect(where, &checkBounds ))
  852.             {
  853.                 if (i==savedPaletteItem) 
  854.                     return kODFalse;
  855.                 
  856.                     // If the user is clicking on a text tool, check to see if we have one.
  857.                     // This also helps when the user was missing a text editor, then got one
  858.                     // and needs the palette to update.
  859.                     if (i==kTextTool)
  860.                     {
  861.                         // Set the text tool flag
  862.                         this->AdjustTextTool(ev);
  863.                     }
  864.                 
  865.                 // Change the palette
  866.                 this->ActivateTool(ev, i);
  867.                 
  868.                 return kODTrue;
  869.             }
  870.         }
  871.     }
  872.  
  873.     return kODFalse;
  874. }
  875.  
  876.         
  877.